package cn.kpt.biz.controller;

import cn.kpt.beans.dto.Dto;
import cn.kpt.beans.pojo.ItripAreaDic;
import cn.kpt.beans.pojo.ItripHotel;
import cn.kpt.beans.pojo.ItripLabelDic;
import cn.kpt.beans.vo.ItripAreaDicVO;
import cn.kpt.beans.vo.ItripLabelDicVO;
import cn.kpt.beans.vo.hotel.HotelVideoDescVO;
import cn.kpt.beans.vo.hotel.ItripSearchDetailsHotelVO;
import cn.kpt.biz.service.AreaDicService;
import cn.kpt.biz.service.HotelService;
import cn.kpt.biz.service.LabelDicService;
import cn.kpt.utils.DtoUtil;
import cn.kpt.utils.EmptyUtils;
import cn.kpt.utils.ErrorCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0
 * @ClassName HotelController
 * @Description TODO
 * @Author kangpengtao
 * @Date 2019/10/16 10:12
 */
@Api(value = "API")
@RestController
@RequestMapping("/api/hotel")
public class HotelController {
    @Resource
    private AreaDicService areaDicService;
    @Resource
    private HotelService hotelService;

    /**
     * 酒店详情-查询酒店政策
     * @param hotelId
     * @return
     */
    @ApiOperation(value = "根据酒店id查询酒店政策", httpMethod = "GET",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class, notes = "根据酒店id查询酒店政策" +
            "<p>成功：success = ‘true’ | 失败：success = ‘false’ 并返回错误码，如下：</p>" +
            "<p>10208: 酒店id不能为空</p>" +
            "<p>10209: 系统异常,获取失败</p>")
    @RequestMapping(value = "/queryhotelpolicy/{hotelId}",method = RequestMethod.GET)
    public Dto doQueryHotelsPolicy(@PathVariable Long hotelId){
        try {
            ItripHotel itripHotel = hotelService.queryHotelById(hotelId);
            if(EmptyUtils.isEmpty(itripHotel)){
                return DtoUtil.returnFail("酒店信息不存在！",ErrorCode.BIZ_PARAMETER_ERROR);
            }
            // 前端使用的是直接解析DTO中的数据 而不是从DTO中解析对象
            return DtoUtil.returnDataSuccess(itripHotel.getHotelPolicy());
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("查询酒店政策列表失败！",ErrorCode.BIZ_UNKNOWN);
        }
    }

    /**
     *根据酒店id查询酒店设施
     * @return
     */
    @ApiOperation(value = "根据酒店id查询酒店设施", httpMethod = "GET",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class, notes = "根据酒店id查询酒店设施" +
            "<p>成功：success = ‘true’ | 失败：success = ‘false’ 并返回错误码，如下：</p>" +
            "<p>10206: 酒店id不能为空</p>" +
            "<p>10207: 系统异常,获取失败</p>")
    @RequestMapping(value = "/queryhotelfacilities/{hotelId}",method = RequestMethod.GET)
    public Dto queryhotelfacilities(@PathVariable Long hotelId){
        try {
            ItripHotel itripHotel = hotelService.queryHotelById(hotelId);
            if(EmptyUtils.isEmpty(itripHotel)){
                return DtoUtil.returnFail("酒店信息不存在！",ErrorCode.BIZ_PARAMETER_ERROR);
            }
            return DtoUtil.returnDataSuccess(itripHotel.getFacilities());
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("查询酒店设施列表失败！",ErrorCode.BIZ_UNKNOWN);
        }

    }

    /**
     * 根据酒店id查询酒店特色和介绍
     * @return
     */
    @ApiOperation(value = "根据酒店id查询酒店特色和介绍", httpMethod = "GET",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class, notes = "根据酒店id查询酒店特色和介绍" +
            "<p>成功：success = ‘true’ | 失败：success = ‘false’ 并返回错误码，如下：</p>" +
            "<p>10210: 酒店id不能为空</p>" +
            "<p>10211: 系统异常,获取失败</p>")
    @RequestMapping(value = "/queryhoteldetails/{hotelId}",method = RequestMethod.GET)
    public Dto queryhoteldetails(@PathVariable Long hotelId){
        try {
            ItripHotel itripHotel = hotelService.queryHotelById(hotelId);
            if(EmptyUtils.isEmpty(itripHotel)){
                return DtoUtil.returnFail("酒店信息不存在！",ErrorCode.BIZ_PARAMETER_ERROR);
            }
            List<ItripSearchDetailsHotelVO> vos = new ArrayList<>();
            // 酒店详情
            ItripSearchDetailsHotelVO vo1 = new ItripSearchDetailsHotelVO();
            vo1.setName("");
            vo1.setDescription(itripHotel.getDetails());
            vos.add(vo1);
            // 酒店特色
            List<ItripLabelDic> itripLabelDics = labelDicService.queryHotelFeatureByHotelId(hotelId);
            for (ItripLabelDic itripLabelDic : itripLabelDics) {
                ItripSearchDetailsHotelVO vo = new ItripSearchDetailsHotelVO();
                vo.setName(itripLabelDic.getName());
                vo.setDescription(itripLabelDic.getDescription());
                vos.add(vo);
            }
            return DtoUtil.returnDataSuccess(vos);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("查询酒店介绍失败！",ErrorCode.BIZ_UNKNOWN);
        }
    }

    /**
     * 酒店详情-查询酒店视频描述
     *
     * @param hotelId
     * @return
     */
    @ApiOperation(value = "根据酒店id查询酒店特色、商圈、酒店名称", httpMethod = "GET",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class, notes = "根据酒店id查询酒店特色、商圈、酒店名称（视频文字描述）" +
            "<p>成功：success = ‘true’ | 失败：success = ‘false’ 并返回错误码，如下：</p>" +
            "<p>错误码：</p>" +
            "<p>100214 : 获取酒店视频文字描述失败 </p>" +
            "<p>100215 : 酒店id不能为空</p>")
    @RequestMapping(value = "/getvideodesc/{hotelId}", method = RequestMethod.GET)
    public Dto getvideodesc(@PathVariable Long hotelId) {
        try {
            ItripHotel itripHotel = hotelService.queryHotelById(hotelId);
            if (EmptyUtils.isEmpty(itripHotel)) {
                return DtoUtil.returnFail("酒店信息不存在！", ErrorCode.BIZ_PARAMETER_ERROR);
            }
            List<ItripLabelDic> itripLabelDics = labelDicService.queryHotelFeatureByHotelId(hotelId);

            HotelVideoDescVO vo = new HotelVideoDescVO();
            // 酒店名
            vo.setHotelName(itripHotel.getHotelName());
            // 酒店商圈  一个酒店对应一个商圈
            ItripAreaDic itripAreaDic = areaDicService.getTradingAreaByHotelId(hotelId);
            String tradingAreaNameList = String.format("【%s】%s", itripAreaDic.getName(), itripHotel.getAddress());
            vo.setTradingAreaNameList(tradingAreaNameList);
            // 酒店特色
            List<String> hotelFeatureList = new ArrayList<>();
            for (ItripLabelDic itripLabelDic : itripLabelDics) {
                hotelFeatureList.add(itripLabelDic.getName());
            }
            vo.setHotelFeatureList(hotelFeatureList);
            return DtoUtil.returnDataSuccess(vo);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("查询酒店视频描述失败！", ErrorCode.BIZ_UNKNOWN);
        }
    }

    @Resource
    private LabelDicService labelDicService;

    /**
     * 酒店列表-查询商业圈
     *
     * @param cityId
     * @return
     */
    @RequestMapping(value = "/querytradearea/{cityId}", method = RequestMethod.GET)
    public Dto doQueryTradeArea(@PathVariable Long cityId) {
        try {
            List<ItripAreaDic> areaDics = areaDicService.queryTradeAreaByCityId(cityId);
            List<ItripAreaDicVO> vos = new ArrayList<>();
            for (ItripAreaDic areaDic : areaDics) {
                ItripAreaDicVO vo = new ItripAreaDicVO();
                BeanUtils.copyProperties(areaDic, vo);
                vos.add(vo);
            }
            return DtoUtil.returnDataSuccess(vos);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("查询商圈列表失败！", ErrorCode.BIZ_UNKNOWN);
        }
    }

    /**
     * 查询热门城市
     *
     * @param isChina
     * @return
     */
    @RequestMapping(value = "/queryhotcity/{isChina}", method = RequestMethod.GET)
    public Dto doQueryHotCity(@PathVariable String isChina) {
        try {
            // 查询信息
            List<ItripAreaDic> areaDics = areaDicService.getHotCity(isChina);

            // 转换
            List<ItripAreaDicVO> vos = new ArrayList<>();
            for (ItripAreaDic areaDic : areaDics) {
                ItripAreaDicVO vo = new ItripAreaDicVO();
                BeanUtils.copyProperties(areaDic, vo);
                vos.add(vo);
            }
            return DtoUtil.returnDataSuccess(vos);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("查询热门城市失败！", ErrorCode.BIZ_UNKNOWN);
        }
    }

    /**
     * 酒店列表-查询酒店特色列表
     *
     * @return
     */
    @ApiOperation(value = "查询酒店特色列表", httpMethod = "GET",
            protocols = "HTTP", produces = "application/json",
            response = Dto.class, notes = "获取酒店特色(用于查询页列表)" +
            "<p>成功：success = ‘true’ | 失败：success = ‘false’ 并返回错误码，如下：</p>" +
            "<p>错误码: </p>" +
            "<p>10205: 系统异常,获取失败</p>")
    @RequestMapping(value = "/queryhotelfeature", method = RequestMethod.GET)
    public Dto queryhotelfeature() {

        try {
//            查询酒店特色列表
            List<ItripLabelDic> labelDics = labelDicService.queryHotelFeature();
//            转换
            List<ItripLabelDicVO> vos = new ArrayList<>();
            for (ItripLabelDic labelDic : labelDics) {
                ItripLabelDicVO vo = new ItripLabelDicVO();
                BeanUtils.copyProperties(labelDic, vo);
                vos.add(vo);
            }
            return DtoUtil.returnDataSuccess(vos);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("查询特色列表失败！", ErrorCode.BIZ_UNKNOWN);
        }
    }
}
